class_name Buff_ChangeMoveSpeedScale
extends BaseBuff

@export var change_value:float
@export var change_scale:float
@export var unique_id:String
@export var unique_id_is_entity_instance_id:bool = false
var modifiers:Array[AttributeModifier] = []

func init_buff(provider:Node,buff_entity:Entity):
	super.init_buff(provider,buff_entity)
	on_get_buff.connect(on_get_buff_callback)
	on_lost_buff.connect(on_lost_buff_callback)
	
func on_get_buff_callback():
	var data:EntityData = cur_buff_entity.entity_data
	if not data:
		return
	var attr:Attribute = data.find_attribute("MoveSpeedScale")
	if attr:
		var cur_unique_id = str(cur_buff_entity.get_instance_id()) if unique_id_is_entity_instance_id else unique_id
		if change_value != 0:
			var modifier:AttributeModifier = AttributeModifier.create(attr,AttributeModifier.OperationType.ADD,change_value,0,{UniqueID = cur_unique_id} if cur_unique_id and not cur_unique_id.is_empty() else null)
			attr.apply_modifier(modifier)
			modifiers.append(modifier)
		if change_scale > 0:
			var modifier:AttributeModifier = AttributeModifier.create(attr,AttributeModifier.OperationType.MUL,change_scale,0,{UniqueID = cur_unique_id} if cur_unique_id and not cur_unique_id.is_empty() else null)
			attr.apply_modifier(modifier)
			modifiers.append(modifier)
func on_lost_buff_callback():
	var data:EntityData = cur_buff_entity.entity_data
	if not data:
		return
	var attr:Attribute = data.find_attribute("MoveSpeedScale")
	if attr:
		Utils.array_for_each(modifiers,func(modifier):attr.remove_modifier(modifier))
